package com.lemons.hackaton.app.data;

import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.filefilter.WildcardFileFilter;

import com.lemons.hackaton.app.Application;
import com.lemons.hackaton.app.util.FindUtils;
import com.lemons.hackaton.obj.Journey;
import com.lemons.hackaton.obj.Person;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.parser.PdfTextExtractor;

public class ReadPdfFiles {
	public String charset = "UTF-8";
	public Map<String, String> aditionalQRInfo = new HashMap<String, String>();

	public static void processPDFS(String filePath)
			throws FileNotFoundException, IOException {
		File dir = new File(filePath);
		FileFilter fileFilter = new WildcardFileFilter("*.pdf");
		File[] files = dir.listFiles(fileFilter);

		for (int i = 0; i < files.length; i++) {
			readPDFData(files[i].toString());
		}
	}

	private static void readPDFData(String pdf) throws IOException {
		
		PdfReader reader = new PdfReader(pdf);
		PdfTextExtractor pdfte = new PdfTextExtractor(reader);
		//System.out.println("READING FILE: " + pdf);
		String pdfText = "";
		for (int i = 0; i < reader.getNumberOfPages(); i++) {
			pdfText += pdfte.getTextFromPage(i + 1);
		}

		processPdfInformation(pdfText);

	}

	private static void processPdfInformation(String pdfText)
			throws NullPointerException {
		Person p = null;
		p = Application.journey.getPerson(FindUtils.findPersonInText(pdfText,
				Journey.personDictionary));
        //System.out.println("p1: " + p);
		if (p != null) {
			p.addToBudget(Float.valueOf(FindUtils.findMoneyInText(pdfText)
					.replace("USD", "")));
			//System.out.println("p2: " + p);
			for (String item : FindUtils.findActivitiesInText(pdfText, Journey.activityDictionary)) {
				p.setActivity(item);
				//System.out.println("p3: " + p);
			}
		}
	}
}
